const path = require('path');
const webpack = require('webpack');
const ParallelUglifyPlugin = require('webpack-parallel-uglify-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

module.exports = {
    getConfig: function (name = 'temp') {
        return {
            resolve: {
                extensions: ['.js', '.jsx']
            },
            entry: {},
            devtool: '#source-map',
            output: {
                path: path.resolve(__dirname, './dist'),
                filename: 'dll.[name].js',
                library: '[name]_[hash]'
            },
            module: {
                rules: [
                    {
                        test: /\.jsx?$/,
                        use: [
                            'babel-loader'
                        ],
                    }
                ],
            },
            plugins: [
                new webpack.DefinePlugin({
                    'process.env.NODE_ENV': JSON.stringify('production'),
                }),
                new ParallelUglifyPlugin({
                    sourceMap: true,
                    uglifyJS: {
                        compress: {
                            unused: true,
                            dead_code: true,
                            warnings: false,
                            pure_funcs: ['console.log'],
                            drop_console: true,
                            drop_debugger: true,
                        },
                        output: {
                            comments: false,
                            beautify: true,
                            keep_quoted_props: true,
                        },
                    },
                }),
                new BundleAnalyzerPlugin({
                    // Can be `server`, `static` or `disabled`.
                    // In `server` mode analyzer will start HTTP server to show bundle report.
                    // In `static` mode single HTML file with bundle report will be generated.
                    // In `disabled` mode you can use this plugin to just generate Webpack Stats JSON file by setting `generateStatsFile` to `true`.
                    analyzerMode: 'static',
                    // Host that will be used in `server` mode to start HTTP server.
                    analyzerHost: '127.0.0.1',
                    // Port that will be used in `server` mode to start HTTP server.
                    analyzerPort: 8888,
                    // Path to bundle report file that will be generated in `static` mode.
                    // Relative to bundles output directory.
                    reportFilename: path.resolve(__dirname, `./dist/webpack_analyzer/dll/report_${name}.html`),
                    // Module sizes to show in report by default.
                    // Should be one of `stat`, `parsed` or `gzip`.
                    // See "Definitions" section for more information.
                    defaultSizes: 'parsed',
                    // Automatically open report in default browser
                    openAnalyzer: true,
                    // If `true`, Webpack Stats JSON file will be generated in bundles output directory
                    generateStatsFile: true,
                    // Name of Webpack Stats JSON file that will be generated if `generateStatsFile` is `true`.
                    // Relative to bundles output directory.
                    statsFilename: path.resolve(__dirname, `./dist/webpack_analyzer/dll/js/stats_${name}.json`),
                    // Options for `stats.toJson()` method.
                    // For example you can exclude sources of your modules from stats file with `source: false` option.
                    // See more options here: https://github.com/webpack/webpack/blob/webpack-1/lib/Stats.js#L21
                    statsOptions: null,
                    // Log level. Can be 'info', 'warn', 'error' or 'silent'.
                    logLevel: 'info'
                  })
            ],
        };
    }
};
